<?php
class User extends MY_Controller
{
	public function __construct(){
		parent::__construct();
		$this->load->library('pagination');
		$this->load->model("model_user");
	}
	public function index(){
		$data['title'] = "List Users";
		$data['template'] = "users/list";
		// Pagination
		$config['base_url'] = base_url().'admin/user/index'; //url page cần PT
		$config['total_rows'] = $this->model_user->totalUsers(); //Total record
		$config['uri_segment'] = 4;
		$config['per_page'] = 3;  // Số record hiển thị trên 1 trang
		$this->pagination->initialize($config);
		$start = $this->uri->segment(4); 
		// End pagination
		$data['listusers'] = $this->model_user->listUsers($config['per_page'],$start);
		//$this->debug($data['listusers']);
		$this->load->view("layout",$data);
	}
	public function addUser(){
		$data['title'] = "Add User";
		$data['template'] = "users/add";
		$data['errors'] = "";
		if($this->input->post("submit") != NULL){
			$this->form_validation->set_rules('name','Tên đăng nhập','required|trim');
			$this->form_validation->set_rules('pass','Mật khẩu','required');
			$this->form_validation->set_rules('repass','Nhập lại mật khẩu', 'required|matches[pass]');
			$this->form_validation->set_rules('phone','Số phone','required|trim|numeric');
			$this->form_validation->set_rules('email','Địa chỉ email', 'required|trim|valid_email');
			if($this->form_validation->run() == FALSE){
				$this->load->view("layout",$data);
			}else{
				$name = $this->input->post("name");
				$pass = $this->input->post("pass");
				$fullname = $this->input->post("fullname");
				$gender = $this->input->post("gender");
				$phone = $this->input->post("phone");
				$email = $this->input->post("email");
				$level = $this->input->post("level");
				$db = array(
						"user_name" => $name,
						"user_pass" => $pass,
						"user_fullname" => $fullname,
						"user_gender" => $gender,
						"user_phone" => $phone,
						"user_email" => $email,
						"user_level" => $level
					  );
				$checkname = $this->model_user->checkuser("user_name",$name);
				$checkemail = $this->model_user->checkuser("user_email",$email);
				if($checkname == FALSE || $checkemail == FALSE){
					$data['errors'] = "Username or Email has been extis";
					$this->load->view("layout",$data);
				}else{
					$this->model_user->addNew($db);
					redirect(base_url()."admin/user");
					exit();
				}
			}
			
		}else{
			$this->load->view("layout",$data);
		}
	}
	public function editUser(){
		$id = $this->uri->segment(4);
		$data['title'] = "Edit User";
		$data['template'] = "users/edit";
		$data['errors'] = "";
		$data['userinfo'] = $this->model_user->getdata($id);
		if($this->input->post("submit") != NULL){
			$this->form_validation->set_rules('name','Tên đăng nhập','required|trim');
			$this->form_validation->set_rules('pass','Mật khẩu','required');
			$this->form_validation->set_rules('repass','Nhập lại mật khẩu', 'required|matches[pass]');
			$this->form_validation->set_rules('phone','Số phone','required|trim|numeric');
			$this->form_validation->set_rules('email','Địa chỉ email', 'required|trim|valid_email');
			if($this->form_validation->run() == FALSE){
				$this->load->view("layout",$data);
			}else{
				$name = $this->input->post("name");
				$pass = $this->input->post("pass");
				$fullname = $this->input->post("fullname");
				$gender = $this->input->post("gender");
				$phone = $this->input->post("phone");
				$email = $this->input->post("email");
				$level = $this->input->post("level");
				$db = array(
						"user_name" => $name,
						"user_pass" => $pass,
						"user_fullname" => $fullname,
						"user_gender" => $gender,
						"user_phone" => $phone,
						"user_email" => $email,
						"user_level" => $level
					  );
				$checkname = $this->model_user->checkuser("user_name",$name,$id);
				$checkemail = $this->model_user->checkuser("user_email",$email,$id);
				if($checkname == FALSE || $checkemail == FALSE){
					$data['errors'] = "Username or Email has been extis";
					$this->load->view("layout",$data);
				}else{
					$this->model_user->update($id,$db);
					redirect(base_url()."admin/user");
					exit();
				}
			}
			
		}else{
			$this->load->view("layout",$data);
		}
	}
	public function del(){
		$id = $this->uri->segment(4);
		$this->model_user->delUser($id);
		redirect(base_url()."admin/user");
		exit();
	}
}